package com.hnit.applet.utils;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * @author zwj
 * @date 2021/10/25
 * @description TODO  Lambda工具类
 */

public class LambdaUtils {

    /**
     * 遍历集合，同时提供item和index值(index从0开始)
     *
     * @param consumer
     * @param <T>
     *
     * @return
     */
    public static <T> Consumer<T> consumerWithIndex(BiConsumer<T, Integer> consumer) {
        class Obj {
            int i;
        }
        Obj obj = new Obj();
        return t -> {
            int index = obj.i++;
            consumer.accept(t, index);
        };
    }
}
